<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/06/04
 * Time: 下午15:32
 */

class ScNotice extends Eloquent
{
    protected $table = "sc_notice";
    public static $status = [0=>"关闭", 1=>"开启"];
    public static $notice_type = [
        1=>"公告", 
        2=>"活动包",  
        3=>"优惠活动", 
        4=>"C端非五星好评",
        5=>"库存预警",
    ];
    public static $product_type = "1";  //活动包默认为线上活动 ，默认为1

    /**
     * @Author:      sqq
     * @DateTime:    2018-06-04 15:35:20
     * @Description: 新建优惠活动站内信
     */
    public static function InsertCouponNotice($id, $name, $desc, $product_type, $begin_date, $end_date, $type, $status, $created_by){
        $notice = New ScNotice();
        $notice->activity_base_id = $id;
        $notice->name = $name;
        $notice->activity_base_type = $type;
        $notice->begin_time = $begin_date;
        $notice->end_time = $end_date;
        $notice->product_type = $product_type;
        $notice->status = $status;
        $notice->desc = $desc;
        $notice->created_at = date("Y-m-d H:i:s",time());
        $notice->updated_at = date("Y-m-d H:i:s",time());
        $notice->notice_type = '3';
        $notice->created_by = $created_by;
        $notice->created_type = '2';
        $notice->save();
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-06-04 15:35:20
     * @Description: 修改优惠活动站内信
     */
    public static function UpdateCouponNotice($id, $name, $desc, $product_type, $begin_date, $end_date, $type, $status, $created_by){
        $notice = ScNotice::where('activity_base_id',$id)->where("notice_type","3")->first();
        if(is_null($notice)){
            ScNotice::InsertCouponNotice($id, $name, $desc, $product_type, $begin_date, $end_date, $type, $status, $created_by);
        }else{
            $notice->activity_base_id = $id;
            $notice->name = $name;
            $notice->activity_base_type = $type;
            $notice->begin_time = $begin_date;
            $notice->end_time = $end_date;
            $notice->product_type = $product_type;
            $notice->status = $status;
            $notice->desc = $desc;
            $notice->updated_at = date("Y-m-d H:i:s",time());
            $notice->notice_type = '3';
            $notice->created_by = $created_by;
            $notice->created_type = '2';
            $notice->save();
        }
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-06-04 17:35:20
     * @Description: 新建活动包站内信
     */
    public static function InsertProductNotice($id, $name, $type, $price, $discount, $status, $begin_time, $end_time, $desc, $is_member, $created_by, $is_edit, $product_type){
        $notice = New ScNotice();
        $notice->activity_base_id = $id;
        $notice->name = $name;
        $notice->type = $type;
        $notice->begin_time = $begin_time;
        $notice->end_time = $end_time;
        $notice->is_member = $is_member;
        $notice->status = $status;
        $notice->desc = $desc;
        $notice->price = $price;
        $notice->discount = $discount;
        $notice->product_type = $product_type;
        $notice->created_at = date("Y-m-d H:i:s",time());
        $notice->updated_at = date("Y-m-d H:i:s",time());
        $notice->notice_type = '2';
        $notice->created_by = $created_by;
        $notice->created_type = '2';
        $notice->is_edit = $is_edit;
        $notice->save();
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-06-04 17:35:20
     * @Description: 修改活动包站内信
     */
    public static function UpdateProductNotice($id, $name, $type, $price, $discount, $status, $begin_time, $end_time, $desc, $is_member, $created_by, $is_edit, $product_type){
        $notice = ScNotice::where('activity_base_id',$id)->where("notice_type","2")->first();
        if(is_null($notice)){
            ScNotice::InsertProductNotice($id, $name, $type, $price, $discount, $status, $begin_time, $end_time, $desc, $is_member, $created_by, $is_edit, $product_type);
        }else{
            $notice->activity_base_id = $id;
            $notice->name = $name;
            $notice->type = $type;
            $notice->begin_time = $begin_time;
            $notice->end_time = $end_time;
            $notice->is_member = $is_member;
            $notice->status = $status;
            $notice->desc = $desc;
            $notice->price = $price;
            $notice->discount = $discount;
            $notice->product_type = $product_type;
            $notice->updated_at = date("Y-m-d H:i:s",time());
            $notice->notice_type = '2';
            $notice->created_by = $created_by;
            $notice->created_type = '2';
            $notice->save();
        }
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-06-06
     * @Description: 查询优惠活动关联站内信是否存在
     */
    public static function Notice($activity_id, $notice_type){
        $notice = ScNotice::where("activity_base_id",$activity_id)->where("notice_type",$notice_type)->select("id")->first();
        if(is_null($notice)){
            return $notice;
        }
        return $notice->toArray();
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-06-06
     * @Description: 站内信优惠活动门店适配
     */
    public static function SaveShop($activity_id, $notice_type, $shop_ids, $created_by, $notice_id){
        ScNoticeShop::where('notice_id',$notice_id)->where('status',1)->update(array('status'=>0));
        foreach($shop_ids as $shop_id){
            $pas=new ScNoticeShop();
            $pas->notice_id = $notice_id;
            $pas->activity_base_id = $activity_id;
            $pas->shop_id = $shop_id;
            $pas->created_by = $created_by;
            $pas->status = '1';
            $pas->created_at = date("Y-m-d H:i:s",time());
            $pas->updated_at = date("Y-m-d H:i:s",time());
            $pas->save();
        }
    }

}